package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.erp.dto.MaterialDto;
import com.yc.sole.erp.entity.Material;
import com.yc.sole.erp.mapper.MaterialMapper;
import com.yc.sole.erp.request.MaterialQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-24
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class MaterialRepository extends ServiceImpl<MaterialMapper, Material> {

    /**
     * 新增
     *
     * @param materialDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveMaterial(MaterialDto materialDto) {
        Material entity = new Material();
        BeanUtil.copyProperties(materialDto, entity);
        this.save(entity);
    }

    /**
     * 通过Id修改
     *
     * @param materialDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateMaterialById(MaterialDto materialDto) {
        Material entity = new Material();
        BeanUtil.copyProperties(materialDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMaterialByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<MaterialDto> selectMaterialByIds(Set<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), MaterialDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public MaterialDto selectMaterialById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), MaterialDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<MaterialDto> selectMaterialPage(Paging page, MaterialQueryReq materialQueryReq) {
        LambdaQueryWrapper<Material> queryWrapper = Wrappers.<Material>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(materialQueryReq.getName()), Material::getName, materialQueryReq.getName());
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), MaterialDto.class);
    }

    /**
     * 根据名称查询
     *
     * @param name
     * @return
     */
    public List<MaterialDto> selectMaterialByName(String name) {
        LambdaQueryWrapper<Material> wrapper = Wrappers.<Material>lambdaQuery().eq(CharSequenceUtil.isNotBlank(name), Material::getName, name);
        return BeanUtil.copyToList(this.list(wrapper), MaterialDto.class);
    }
}
